﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Quadrant.domain.Helpers;
using Quadrant.domain.Models;

namespace Quadrant.domain.Services
{
    public class ZoneServices : BaseServices
    {
        private readonly List<Tuple<Byte, String, String>> StandardQuadrant = new List<Tuple<byte, string, string>>(4)
            {
                new Tuple<byte, string, string>(0, "Urgent", "Do these, when done, spend time to think about how to deal with the situation in the future."),
                 new Tuple<byte, string, string>(1, "Not urgent", "While not urgent, all your available time should go to this quadrant."),
                 new Tuple<byte, string, string>(2, "Important", "While not urgent, all your available time should go to this quadrant."),
                 new Tuple<byte, string, string>(3, "Not Important", "Don't do these, if you spend a lot of time here, stop doing it and start spending time in the 'not urgent and important' zone."),
            };

        private readonly List<Tuple<Byte, String, String>> AgileQuadrant = new List<Tuple<byte, string, string>>(4)
            {
                new Tuple<byte, string, string>(0, "Urgent", "Do it!"),
                 new Tuple<byte, string, string>(1, "Not urgent", "Schedule them"),
                 new Tuple<byte, string, string>(2, "Important", "Delegate them"),
                 new Tuple<byte, string, string>(3, "Not Important", "Give up ?"),
            };

        private readonly List<Tuple<Byte, String, String>> TaskQuadrant = new List<Tuple<byte, string, string>>(4)
            {
                new Tuple<byte, string, string>(0, "Immediate", "Important tasks that are due now."),
                 new Tuple<byte, string, string>(1, "Not Immediate", "Important tasks needs planning."),
                 new Tuple<byte, string, string>(2, "Critical", "Delegate them."),
                 new Tuple<byte, string, string>(3, "Not Critical", "Don't do these"),
            };

        public ZoneServices(Logger logger) : base(logger) { }

        public ZoneServices() : base() { }

        public List<Tuple<Byte, String, String>> GetQuadrantModel(int model)
        {
            return this.StandardQuadrant;
        }

        public Task<Zone[]> GetZones(Guid userId, Guid? qId = null)
        {

            var qry = this.DB.Zones.AsNoTracking().Where(p => p.Quadrant.OwnerId == userId);
            if (qId != null)
            {
                qry = qry.Where(p => p.QuadrantId == qId.Value);
            }
            return qry.ToArrayAsync();
        }

        public Task<Zone> GetZone(long id)
        {
            return this.DB.Zones.AsNoTracking().SingleAsync(p => p.Id == id);
        }

        public async Task<OpResult> SaveZone(Zone zone)
        {
            try
            {
                this.DB.Entry(zone).State = EntityState.Modified;
                await this.DB.SaveChangesAsync();
                return new OpResult { Id = zone.Id, Success = true };
            }
            catch (Exception ex)
            {
                this.Logger.Error("ZoneServices: " + ex.Message, ex);
                return new OpResult { Id = zone.Id, Success = false };
            }

        }
    }
}
